cmake_minimum_required(VERSION 2.8)

project(akka)

# where to find cmake modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
"${CMAKE_SOURCE_DIR}/utils/cmake_modules")

# find git, in ${GIT_EXECUTABLE}
find_package(Git REQUIRED)

# find pandoc, in ${PANDOC_BIN}
find_package(Pandoc REQUIRED)

# find latex, in ${PDFLATEX_BIN} and ${XELATEX_BIN}
find_package(Latex)

# kindlegen
find_package(Kindlegen)

# 
IF(CMAKE_SYSTEM_NAME STREQUAL Linux)
  set(WC_BIN "wc")
ELSEIF(CMAKE_SYSTEM_NAME STREQUAL Darwin)
  set(WC_BIN gwc)
ELSEIF(CMAKE_SYSTEM_NAME STREQUAL Windows)
  MESSAGE(FATAL "Untested platform")
ENDIF()

# the name of the output file
set(TARGET_FILENAME "akka")

execute_process(COMMAND ${GIT_EXECUTABLE} log --pretty=oneline --no-color
                COMMAND ${WC_BIN} -l
                RESULT_VARIABLE RESULT_RETURNED
                OUTPUT_VARIABLE FILE-VERSION
                OUTPUT_STRIP_TRAILING_WHITESPACE
                )
#MESSAGE(STATUS "FILE-VERSION is ${FILE-VERSION}")

execute_process(COMMAND ${GIT_EXECUTABLE} log --pretty=oneline --no-color
                COMMAND head -1
                COMMAND cut -b -10
                RESULT_VARIABLE RESULT_RETURNED
                OUTPUT_VARIABLE GIT-VERSION
                OUTPUT_STRIP_TRAILING_WHITESPACE
                )
#MESSAGE(STATUS "GIT-VERSION is ${GIT-VERSION}")

# chapter 00
file(GLOB FILE_LISTS_00 *.md)
set(FILE_LISTS_ALL ${FILE_LISTS_ALL} ${FILE_LISTS_00})

# chapter 01
file(GLOB FILE_LISTS_01
  ch01-Introducing_Akka/*.md)
set(FILE_LISTS_ALL ${FILE_LISTS_ALL} ${FILE_LISTS_01})

# chapter 02
file(GLOB FILE_LISTS_02
  ch02-Up_and_Running/*.md)
set(FILE_LISTS_ALL ${FILE_LISTS_ALL} ${FILE_LISTS_02})

# chapter 03
file(GLOB FILE_LISTS_03
  ch03-Test_Driven_Development_with_Actors/*.md)
set(FILE_LISTS_ALL ${FILE_LISTS_ALL} ${FILE_LISTS_03})

# chapter 04
file(GLOB FILE_LISTS_04
  ch04-Fault_tolerance/*.md)
set(FILE_LISTS_ALL ${FILE_LISTS_ALL} ${FILE_LISTS_04})

# chapter 05
file(GLOB FILE_LISTS_05
  ch05-futures/*.md)
set(FILE_LISTS_ALL ${FILE_LISTS_ALL} ${FILE_LISTS_05})

# chapter 06
file(GLOB FILE_LISTS_06
  ch06-Your_first_Distributed_Akka_App/*.md)
set(FILE_LISTS_ALL ${FILE_LISTS_ALL} ${FILE_LISTS_06})

# sort the chapter and section list, convert list into string
list(SORT FILE_LISTS_ALL)
# convert list into string
string(REPLACE ";" "\t" FILES_STRING  "${FILE_LISTS_ALL}")

# copy figures
add_custom_target(cp-figs
                COMMAND cp -r ../figures .
)

# generate html format
add_custom_target(html ALL
COMMAND ${PANDOC_BIN} ${FILES_STRING} -o ${TARGET_FILENAME}-${FILE-VERSION}-${GIT-VERSION}.html
DEPENDS ${FILE_LISTS_ALL}
)
add_dependencies(html cp-figs)

# generate epub format
add_custom_target(epub
  COMMAND ${PANDOC_BIN} ${FILES_STRING} -o ${TARGET_FILENAME}-${FILE-VERSION}-${GIT-VERSION}.epub
  DEPENDS ${FILE_LISTS_ALL})
add_dependencies(epub cp-figs)

# generate mobi format if KINDLEGEN_FOUND
If(KINDLEGEN_FOUND)
  add_custom_target(mobi
    COMMAND cat ../utils/template/mobi/akka_header.html > akka.html
		COMMAND cat ${TARGET_FILENAME}-${FILE-VERSION}-${GIT-VERSION}.html >> akka.html
    COMMAND cat ../utils/template/mobi/akka_footer.html >> akka.html
    COMMAND kindlegen akka.html
    DEPENDS ${FILE_LISTS_ALL})
  add_dependencies(mobi cp-figs)
  add_dependencies(mobi html)
EndIf(KINDLEGEN_FOUND)

# generate pdf format if PDFLATEX_FOUND
If(PDFLATEX_FOUND)
  add_custom_target(pdf
    COMMAND cp ../utils/template/pdflatex/akka.tex .
    COMMAND pandoc ${FILE_LISTS_ALL} -o content.tex
    COMMAND sed -i 's/\\label{.*$///' content.tex
    COMMAND ${PDFLATEX_BIN} akka.tex
    COMMAND mv akka.pdf ${TARGET_FILENAME}-${FILE-VERSION}-${GIT-VERSION}.pdf  
    DEPENDS ${FILE_LISTS_ALL})
  add_dependencies(pdf cp-figs)
EndIf(PDFLATEX_FOUND)
